//
//  UIViewController+HUD.m
//  MyFramework
//
//  Created by 余洋 on 2018/7/24.
//  Copyright © 2018年 余洋. All rights reserved.
//

#import "UIViewController+HUD.h"
#import "MBProgressHUD.h"
#import "NSObject+Current.h"
#import <UIImage+GIF.h>
#import "LoadingView.h"

@implementation UIViewController (HUD)

-(void)lc_showToastMessage:(NSString*)message{
    [self lc_showToastMessage:message delay:1.5f];
}

-(void)lc_showToastMessageSuccess:(NSString*)message{
    [self lc_showToastMessage:message imageName:@"Fill" delay:1.5f];
}

-(void)lc_showToastMessageGif:(NSString *)message {
    [self lc_showToastMessage:message gifName:@"catloading" delay:5.f];
}

-(void)lc_showToastMessage:(NSString*)message delay:(float) delay{
    UIApplication* application = [UIApplication sharedApplication];
    //把之前的隐藏
    MBProgressHUD* toastHud = [MBProgressHUD HUDForView:application.delegate.window];
    if (toastHud) {
        [toastHud hideAnimated:NO];
        toastHud = nil;
    }
    toastHud = [[MBProgressHUD alloc] initWithView:application.delegate.window];
    toastHud.removeFromSuperViewOnHide = YES;
    [application.keyWindow addSubview:toastHud];
    toastHud.mode = MBProgressHUDModeText;
    toastHud.detailsLabel.text = message;
    toastHud.detailsLabel.font = [UIFont systemFontOfSize:16.f];
    toastHud.detailsLabel.textColor = [UIColor whiteColor];
    UIView *keyboard = [NSObject lc_findKeyboard];
    if (keyboard == nil) {
        toastHud.offset = CGPointMake(0, MBProgressMaxOffset);
    } else {
        toastHud.offset = CGPointMake(0, [UIScreen mainScreen].bounds.size.height/2.f - CGRectGetHeight(keyboard.frame) - 90);
    }
    //背景框颜色
    toastHud.bezelView.color = [UIColor blackColor];
    [toastHud showAnimated:YES];
    [toastHud hideAnimated:YES afterDelay:delay];
    toastHud.userInteractionEnabled = NO;
}

-(void)lc_showToastMessage:(NSString *)message imageName:(NSString *)imageName delay:(float) delay {
    UIApplication* application = [UIApplication sharedApplication];
    //把之前的隐藏
    MBProgressHUD* toastHud = [MBProgressHUD HUDForView:application.delegate.window];
    if (toastHud) {
        [toastHud hideAnimated:NO];
        toastHud = nil;
    }
    toastHud = [[MBProgressHUD alloc] initWithView:application.delegate.window];
    toastHud.removeFromSuperViewOnHide = YES;
    //背景框颜色
    toastHud.bezelView.color = [UIColor blackColor];
    [application.keyWindow addSubview:toastHud];
    toastHud.mode = MBProgressHUDModeCustomView;
    toastHud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    toastHud.detailsLabel.text = message;
    toastHud.detailsLabel.font = [UIFont systemFontOfSize:16.f];
    toastHud.detailsLabel.textColor = [UIColor whiteColor];
    toastHud.offset = CGPointMake(0, -getRectNavAndStatusHight/2);
    [toastHud showAnimated:YES];
    [toastHud hideAnimated:YES afterDelay:delay];
    toastHud.userInteractionEnabled = NO;
}

-(void)lc_showToastMessage:(NSString *)message gifName:(NSString *)gifName delay:(float) delay {
    UIApplication* application = [UIApplication sharedApplication];
    //把之前的隐藏
    MBProgressHUD* toastHud = [MBProgressHUD HUDForView:application.delegate.window];
    if (toastHud) {
        [toastHud hideAnimated:NO];
        toastHud = nil;
    }
    toastHud = [[MBProgressHUD alloc] initWithView:application.delegate.window];
    toastHud.removeFromSuperViewOnHide = YES;
    //背景框颜色
    toastHud.bezelView.color = [UIColor clearColor];
    [application.keyWindow addSubview:toastHud];
    toastHud.mode = MBProgressHUDModeCustomView;
    toastHud.offset = CGPointMake(0, -getRectNavAndStatusHight/2);
    LoadingView *view = [[[NSBundle mainBundle] loadNibNamed:@"LoadingView" owner:nil options:nil] firstObject];
    toastHud.customView = view;
    toastHud.detailsLabel.text = message;
    toastHud.detailsLabel.font = [UIFont systemFontOfSize:16.f];
    [toastHud showAnimated:YES];
    [toastHud hideAnimated:YES afterDelay:delay];
}

@end
